package com.stlm2.controller;

import com.stlm2.dems.dao.*;
import com.stlm2.dems.entity.*;
import com.stlm2.dems.service.*;
import com.stlm2.dems.user.entity.Login;
import com.stlm2.dems.user.entity.UserState;
import com.stlm2.util.Convert;
import com.stlm2.util.ExcelUtils;
import com.stlm2.util.Memcached;
import com.stlm2.util.codecontent.CodeContent;
import net.sf.json.JSONArray;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.Collections;

/**
 * Created by sfy on 2017/10/24.
 */
@Controller
@RequestMapping("/purchaseOrderC")
public class PurchaseOrderController {

    @Resource
    private Memcached memcached;
    @Resource
    private PurchaseOrderService purchaseOrderService;
    @Resource
    private AddressDao addressDao;
    @Resource
    private AddressService addressService;
    @Resource
    private UserCardsService userCardsService;
    @Resource
    private UserDao userDao;
    @Resource
    private GoodSpecificationDao goodSpecificationDao;
    @Resource
    private DdmShopDao ddmShopDao;
    @Resource
    private GoodsValueAddedServicesService goodsValueAddedServicesService;
    @Resource
    private ValueAddedServicesService valueAddedServicesService;
    @Resource
    private DdmShoppingCartDao ddmShoppingCartDao;
    @Resource
    private ShopOrderService shopOrderService;
    @Resource
    private GoodsOrderDao goodsOrderDao;
    @Resource
    private GoodSpecificationDao getGoodSpecificationDao;
    @Resource
    private DdmGoodsDao ddmGoodsDao;
    @Resource
    private UserService userService;
    @Resource
    private MessagesDao messagesDao;
    @Resource
    private RefundLogService refundLogService;

    /*
    * sfy
    * 2017年10月24日09:33:10
    * 立即购买，生成一个预售单
    * params type orderInfor "[{},{}]"
    * */
    @ResponseBody
    @RequestMapping(value = "/buyRightNow.json", produces = "application/json;charset=UTF-8")
    public String buyRightNow(String companyCode,String token, String orderInfor) {
        int type = 1;//立即购买
        Map<String, Object> map = new HashMap<String, Object>();
             /*
          * 是否登录
          * */
        if (StringUtils.isEmpty(token)) {
            map.put("code", -1);
            map.put("msg", "token参数不能为空");
            return new JSONObject(map).toString();
        }
        User loginUser = userDao.getByToken(token);
        if (loginUser == null) {
            map.put("code", -1);
            map.put("msg", "请登录");
            return new JSONObject(map).toString();
        }

    /*
        * 处理用户收货地址问题
        * */
        //用户是否已经设置收货地址
        Address userAddress = addressDao.getByUserId(loginUser.getId());
        if (userAddress == null) {
            map.put("addressType", 1);
            map.put("addressContent", "请填写您的收货地址");
        } else {
        /*
        *地址参数精简
        * */
            Map<String, Object> addressMap = new HashMap<String, Object>();
            addressMap = addressService.buildModel(userAddress);
            map.put("address", addressMap);
            map.put("addressType", 2);
        }
        /*
        * 处理商品清单以及清单的商品数据、商品数量、清单的总价
        * */
        Map<String, Object> mapPre = new HashMap<String, Object>();
        mapPre = purchaseOrderService.preOrder(type, orderInfor, loginUser.getId());
        if (!mapPre.get("code").toString().equals("1")) {
            return CodeContent.getModelByCode(Integer.valueOf(mapPre.get("code").toString()), mapPre.get("msg").toString(), null);
        }
        List<Map<String, Object>> goodList = (List<Map<String, Object>>) mapPre.get("goodsList");

        BigDecimal allPrice = (BigDecimal) mapPre.get("allPrice");
        map.put("goodsList", goodList);

        /*
        * 可以使用的卡券
        * */
        Map<String, Object> cardMap = userCardsService.validAndUserable(loginUser.getId(), orderInfor);

        if (!cardMap.get("code").toString().equals("1")) {
            return CodeContent.getModelByCode(Integer.valueOf(cardMap.get("code").toString()), cardMap.get("msg").toString(), null);
        }

        BigDecimal cardMoney = new BigDecimal(0);
        List<UserCards> useAbleCardList = (List<UserCards>) cardMap.get("useAbleList");
        List<UserCards> unUseAbleCardList = (List<UserCards>) cardMap.get("unUseAbeList");
        List<UserCards> timeOutList = (List<UserCards>) cardMap.get("timeOutList");
        int useAbleNum = (Integer) cardMap.get("useAbleNum");
        int unUseAbleNum = (Integer) cardMap.get("unUseAbleNum");
        if (useAbleCardList != null && useAbleCardList.size() > 0) {
            for (UserCards userCards : useAbleCardList) {
                cardMoney = cardMoney.add(userCards.getMoney());
            }
        }
        Map<String, Object> cardInfo = new HashMap<String, Object>();
        cardInfo.put("num", useAbleCardList.size());
        cardInfo.put("money", cardMoney);

        /*
        * 合计金钱
        * */
        map.put("allPrice", allPrice);
        map.put("useAbleNum", useAbleNum);
        map.put("unUseAbleNum", unUseAbleNum);
        BigDecimal endPrice = allPrice.subtract(cardMoney);
        map.put("endPrice", endPrice);
        map.put("cardNumAndMoney", cardInfo);
        map.put("useAbleList", userCardsService.convert_simplify(useAbleCardList));
        map.put("unUseAbleList", userCardsService.convert_simplify(unUseAbleCardList));
        map.put("timeOutList", userCardsService.convert_simplify(timeOutList));
        map.put("useCardsIdList", cardMap.get("useCardsIdList"));
        return new JSONObject(map).toString();

    }

    /*
* sfy
* 2017年10月24日09:33:10
* 购物车商品去结算，生成一个预售单
* params type orderInfor "[{},{}]"
* */
    @ResponseBody
    @RequestMapping(value = "/cartsBuy.json", produces = "application/json;charset=UTF-8")
    public String cartsBuy(String companyCode,String token) {
        int type = 2;//购物车去结算
        Map<String, Object> map = new HashMap<String, Object>();
             /*
          * 是否登录
          * */
        if (StringUtils.isEmpty(token)) {
            map.put("code", -1);
            map.put("msg", "token参数不能为空");
            return new JSONObject(map).toString();
        }
        User loginUser = userDao.getByToken(token);
        if (loginUser == null) {
            map.put("code", -1);
            map.put("msg", "请登录");
            return new JSONObject(map).toString();
        }

    /*
        * 处理用户收货地址问题
        * */
        //用户是否已经设置收货地址
        Address userAddress = addressDao.getByUserId(loginUser.getId());
        if (userAddress == null) {
            map.put("addressType", 1);
            map.put("addressContent", "请填写您的收货地址");
        } else {
        /*
        *地址参数精简
        * */
            Map<String, Object> addressMap = new HashMap<String, Object>();
            addressMap = addressService.buildModel(userAddress);
            map.put("address", addressMap);
            map.put("addressType", 2);
        }
        /*
        * 处理商品清单以及清单的商品数据、商品数量、清单的总价
        * */
        Map<String, Object> mapPre = new HashMap<String, Object>();
        mapPre = purchaseOrderService.preOrder(type, "", loginUser.getId());//因为是购物车去结算，所以orderInfo信息为空


        if (!mapPre.get("code").toString().equals("1")) {
            return CodeContent.getModelByCode(Integer.valueOf(mapPre.get("code").toString()), mapPre.get("msg").toString(), null);
        }
        List<Map<String, Object>> goodList = (List<Map<String, Object>>) mapPre.get("goodsList");

        BigDecimal allPrice = (BigDecimal) mapPre.get("allPrice");
        map.put("goodsList", goodList);

        /*
        * 可以使用的卡券，以及不可以使用的优惠券，以及过期的优惠券
        * */
        Map<String, Object> cardMap = userCardsService.validAndUserable(loginUser.getId(), "");//购物的orderinfo信息从数据库中去获取


        BigDecimal cardMoney = new BigDecimal(0);
        List<UserCards> useAbleCardList = (List<UserCards>) cardMap.get("useAbleList");
        List<UserCards> unUseAbleCardList = (List<UserCards>) cardMap.get("unUseAbeList");
        List<UserCards> timeOutList = (List<UserCards>) cardMap.get("timeOutList");
        int useAbleNum = (Integer) cardMap.get("useAbleNum");
        int unUseAbleNum = (Integer) cardMap.get("unUseAbleNum");
        if (useAbleCardList != null && useAbleCardList.size() > 0) {
            for (UserCards userCards : useAbleCardList) {
                cardMoney = cardMoney.add(userCards.getMoney());
            }
        }
        Map<String, Object> cardInfo = new HashMap<String, Object>();
        cardInfo.put("num", useAbleCardList.size());
        cardInfo.put("money", cardMoney);

        /*
        * 合计金钱
        * */
        map.put("allPrice", allPrice);
        map.put("useAbleNum", useAbleNum);
        map.put("unUseAbleNum", unUseAbleNum);
        BigDecimal endPrice = allPrice.subtract(cardMoney);
        map.put("endPrice", endPrice);
        map.put("cardNumAndMoney", cardInfo);
        map.put("useAbleList", userCardsService.convert_simplify(useAbleCardList));
        map.put("unUseAbleList", userCardsService.convert_simplify(unUseAbleCardList));
        map.put("timeOutList", userCardsService.convert_simplify(timeOutList));
        map.put("useCardsIdList", cardMap.get("useCardsIdList"));
        return new JSONObject(map).toString();

    }

    /*
    * sfy
    * 2017年11月2日16:28:26
    * 提交订单接口
    * */
    @ResponseBody
    @RequestMapping(value = "/submitOrder.json", produces = "application/json;charset=UTF-8")
    public String submitOrder(String companyCode,String token, Integer type, String orderInfor, Integer addressId, String userCardsId, String memo, Integer deliveryType, Integer storeId, Integer payType) {
        Map<String, Object> map = new HashMap<String, Object>();
        Address address = null;
        if (StringUtils.isEmpty(token)) {
            map.put("code", -1);
            map.put("msg", "token参数不能为空");
            return new JSONObject(map).toString();
        }
        User loginUser = userDao.getByToken(token);
        if (loginUser == null) {
            map.put("code", -1);
            map.put("msg", "请登录");
            return new JSONObject(map).toString();
        }
        if ((type == null) || (type != 1 && type != 2)) {
            map.put("code", 0);
            map.put("msg", "type参数异常");
            return new JSONObject(map).toString();

        }
        //type = 1 表示是立即购买type=2表示是购物车购买
        if (type == 1 && StringUtils.isEmpty(orderInfor)) {
            map.put("code", 0);
            map.put("msg", "立即购买必须有orderinfor信息");
            return new JSONObject(map).toString();
        }


        if (StringUtils.isEmpty(deliveryType)) {
            map.put("code", 0);
            map.put("msg", "无发货方式");
            return new JSONObject(map).toString();
        }
        if (StringUtils.isEmpty(payType)) {
            map.put("code", 0);
            map.put("msg", "请选择支付方式");
            return new JSONObject(map).toString();
        }
        if(deliveryType==2){
            if (addressId == null) {
                map.put("code", 3);
                map.put("msg", "您还未设置地址请先设置地址");
                return new JSONObject(map).toString();
            } else {
                //判断地址是否有效
                address = addressDao.getById(addressId);

                if (address == null) {
                    map.put("code", 0);
                    map.put("msg", "您的地址无效");
                    return new JSONObject(map).toString();
                } else if (address.getUserId()-loginUser.getId()!=0) {
                    map.put("code", 0);
                    map.put("msg", "userid:" + loginUser.getId() + "没有地址id:" + addressId);
                    return new JSONObject(map).toString();
                }

            }
        }

        DdmShop ddmShop = null;


        //预设一级订单对象
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        String pCode = CodeContent.gCode("p", loginUser.getId());
        purchaseOrder.setMemo(memo);
        purchaseOrder.setCreateTime(new Date());
        if (address!=null){
            purchaseOrder.setAddressDetail(address.getDetail());
            purchaseOrder.setCity(address.getCity());
            purchaseOrder.setPhone(address.getPhone());
            purchaseOrder.setName(address.getName());
        }else{
            purchaseOrder.setName(loginUser.getRealName());
            purchaseOrder.setPhone(loginUser.getPhone());

        }

        purchaseOrder.setCode(pCode);
        purchaseOrder.setDeliveryType(DeliveryType.getByIntValue(deliveryType));
        purchaseOrder.setOrderState(OrderState.Create);
        purchaseOrder.setUserId(loginUser.getId());
        purchaseOrder.setSeckill(false);
        purchaseOrder.setPayType(PayType.getByIntValue(payType));

        //预设二级订单
       /*
       * 由于公司业务问题，所以当前默认为一个主仓库，
       * 根据店铺Id决定此订单归属问题
       * */
        DdmShop wareHouseShop = null;
        try {
            wareHouseShop = ddmShopDao.wareShop();
            if (wareHouseShop == null) {
                map.put("code", 0);
                map.put("msg", "获取的仓库对象为空");
                return new JSONObject(map).toString();
            }
        } catch (Exception e) {
            map.put("code", 0);
            map.put("msg", "获取仓库异常");
            return new JSONObject(map).toString();
        }

        ShopOrder shopOrder = new ShopOrder();
        shopOrder.setOrderState(OrderState.Create);
        String cOrder = CodeContent.gCode("c", loginUser.getId());
        shopOrder.setCode(cOrder);
        shopOrder.setUserId(loginUser.getId());
        shopOrder.setDeliveryType(DeliveryType.getByIntValue(deliveryType));
        shopOrder.setCreateTime(new Date());
       shopOrder.setPurchaseOrderCode(purchaseOrder.getCode());
       shopOrder.setSeckill(false);
       shopOrder.setPayType(PayType.getByIntValue(payType));
        if(StringUtils.isEmpty(storeId)){//表示没有把订单制定到某个门店
            shopOrder.setShopId(wareHouseShop.getId());

        } else {//表示需要把这个订单给某个店铺
            shopOrder.setShopId(storeId);
            ddmShop = ddmShopDao.getById(storeId);
            if (ddmShop == null) {
                map.put("code", 0);
                map.put("msg", "您获取的商店对象不存在");
                return new JSONObject(map).toString();
            }

        }
        //当前默认都是自营
        shopOrder.setType(1);
       /*
       * 准备3级订单的集合
       * */
        List<GoodsOrder> goodsOrderList = new ArrayList<GoodsOrder>();
  /*
           * 获取传递过来的卡券集合      01
           * */
        Map<String, Object> cardsMap = new HashMap<String, Object>();
        List<UserCards> userCardsList = new ArrayList<UserCards>();
        if (!StringUtils.isEmpty(userCardsId)) {//表示订单中有使用卡券的情况
            cardsMap = userCardsService.getValidCards(userCardsId, loginUser.getId());


            if (!cardsMap.get("code").toString().equals("1")) {//表示调用的service有一些问题
                return CodeContent.getModelByCode(Integer.valueOf(cardsMap.get("code").toString()), cardsMap.get("msg").toString(), null);
            } else {//表示内部service没有任何问题
                userCardsList = (List<UserCards>) cardsMap.get("userCardList");
            }
        }

        //购买订单 购买增值服务个数
        int purchaseOrderServiceNum=0;

        //店铺订单 购买增值服务个数  先在无拆分订单  购买订单 购买增值服务个数相同   后期如有拆单 需调整
        int shopOrderServiceNum=0;
        if (type == 1 && !StringUtils.isEmpty(orderInfor)) {//立即购买

             /*
             * 结合orderinfor，查看用户传递过来的卡券是否是此订单可用的
             * */

            JSONArray jsonArray = null;
            try {
                jsonArray = JSONArray.fromObject(orderInfor);//并将DS内容取出转为json数组
            } catch (Exception e) {
                map.put("code", 0);
                map.put("msg", "orderInfor解析出错，请确认您的orderInfor格式正确");
                return new JSONObject(map).toString();
            }

            for (int i = 0; i < jsonArray.size(); i++) {     //遍历json数组内容
                net.sf.json.JSONObject object = jsonArray.getJSONObject(i);
                int goodId;
                try {
                    goodId = object.getInt("goodId");
                } catch (Exception e) {
                    map.put("code", 0);
                    map.put("msg", "获取商品id异常");
                    return new JSONObject(map).toString();
                }
                int goodNum;
                try {
                    goodNum = object.getInt("num");
                } catch (Exception e) {
                    map.put("code", 0);
                    map.put("msg", "获取商品数量出错");
                    return new JSONObject(map).toString();
                }
                if (goodNum <= 0) {
                    map.put("code", 0);
                    map.put("msg", "商品数量不能小于0或者等于0");
                    return new JSONObject(map).toString();
                }

                int gooodSpecificationId = 0;


                String addService = "";

                try {
                    gooodSpecificationId = object.getInt("goodSpecificationId");
                } catch (Exception e) {
                    gooodSpecificationId = 0;//表示没有规格
                }
                try {
                    addService = object.getString("addService");
                } catch (Exception e) {
                    addService = "";
                }

                //处理商品清单
                //从数据库中获取商品对象
                DdmGoods good = ddmGoodsDao.getById(goodId);


                if (good == null) {
                    map.put("code", 0);
                    map.put("msg", "商品不存在");
                    return new JSONObject(map).toString();
                }

                if (good.getState() == false) {
                    map.put("code", 0);
                    map.put("msg", "商品状态无效");
                    map.put("msg", "无效商品");
                    return new JSONObject(map).toString();
                }

                boolean ifIslime = good.getIsImie();//商品是否必须一个唯一序列号


                  /*
                  * 处理规格
                  * */
                if (!StringUtils.isEmpty(gooodSpecificationId) && gooodSpecificationId != 0) {//带规格的商品
                    GoodSpecification goodSpecification = goodSpecificationDao.getById(gooodSpecificationId);

                    if (goodSpecification == null) {
                        map.put("code", 0);
                        map.put("msg", "商品规格不存在");
                        return new JSONObject(map).toString();
                    }
                    if (goodId != goodSpecification.getGoodId()) {
                        map.put("code", 0);
                        map.put("msg", "商品和商品规格对象不一致");
                        return new JSONObject(map).toString();

                    }
                    //根据商品是否是需要mine号来进行不同的处理
                    if (!ifIslime) {//如果产品不需要    产品理应就没有增值服务
                        //判断是否有增服务
                        if (!StringUtils.isEmpty(addService)) {//表示需要增值服务
                            map.put("code", 0);
                            map.put("msg", "商品id为" + good.getId() + "的商品，基础属性不需要mine号，却有增值服务矛盾");
                            return new JSONObject(map).toString();
                        }

                        //预设3级订单
                        GoodsOrder goodsOrder = new GoodsOrder();
                        goodsOrder.setOrderState(OrderState.Create);
                        goodsOrder.setCreateTime(new Date());
                        goodsOrder.setGoodsName(good.getName());
                        goodsOrder.setUserId(loginUser.getId());
                        goodsOrder.setSeckill(false);
                        goodsOrder.setGoodsSpecificationId(gooodSpecificationId);
                        //无增值服务不需要进行相关增值服务的设置
                        goodsOrder.setNum(goodNum);
                          /*
                          * 处理用户的卡券
                          * */
                        UserCards userCards = userCardsService.goodCanUseUserCard(goodId, userCardsList);
                        //由于商品没有mine号，所以商品的增值服务为0
                        BigDecimal totalMoney = goodSpecification.getMemberPrice().multiply(new BigDecimal(goodNum));
                        BigDecimal payMoney = goodSpecification.getMemberPrice().multiply(new BigDecimal(goodNum));
                        BigDecimal saveMoney = new BigDecimal(0);//初始话没有省钱
                        if (userCards != null) {//表示此商品有可用的券
                            goodsOrder.setCardId(userCards.getId());
                            payMoney = payMoney.subtract(userCards.getMoney());//减去卡券的钱
                            saveMoney = userCards.getMoney();
                            //去除掉这个卡券
                            userCardsService.removeUserCards(userCards, userCardsList);
                        } else {
                            goodsOrder.setSaveMoney(saveMoney);
                        }
                        goodsOrder.setDdPrice(goodSpecification.getDdPrice());
                        goodsOrder.setHasLmie(false);
                        goodsOrder.setMarketPrice(goodSpecification.getMarketPrice());
                        goodsOrder.setGoodsId(goodId);
                        goodsOrder.setSalePrice(goodSpecification.getMemberPrice());
                        goodsOrder.setPurchaseOrderCode(purchaseOrder.getCode());
                        goodsOrder.setSaveMoney(saveMoney);
                        goodsOrder.setShopOrderCode(shopOrder.getCode());
                        goodsOrder.setPayMoney(payMoney);
                        goodsOrder.setTotalMoney(totalMoney);
                        goodsOrder.setServiceNum(0);
                        String iCode = CodeContent.gCode("i", loginUser.getId());
                        goodsOrder.setCode(iCode);
                        //将预设的三级订单加入到集合中，为之后做二级订单的构造做准备
                        goodsOrderList.add(goodsOrder);


                    } else {//此商品需要mine号

                        for (int k = 0; k < goodNum; k++) {//将每一个商品分拆为一个订单
                            GoodsOrder goodsOrder = new GoodsOrder();
                            BigDecimal totalMoney = new BigDecimal(0);
                            BigDecimal totalPayMoney = new BigDecimal(0);
                            BigDecimal totalSaveMoney = new BigDecimal(0);
                            BigDecimal totalServiceMoney = new BigDecimal(0);
                            String code = CodeContent.gCode("i", loginUser.getId() + k);
                            goodsOrder.setCode(code);
                            goodsOrder.setShopOrderCode(shopOrder.getCode());
                            goodsOrder.setOrderState(OrderState.Create);
                            goodsOrder.setUserId(loginUser.getId());
                            goodsOrder.setSeckill(false);
                            UserCards userCards = userCardsService.goodCanUseUserCard(goodId, userCardsList);
                            //去除掉这个卡券
                            if(userCards!=null){
                                goodsOrder.setCardId(userCards.getId());
                                totalSaveMoney = userCards.getMoney();
                                goodsOrder.setSaveMoney(totalSaveMoney);
                                userCardsService.removeUserCards(userCards, userCardsList);
                            } else {
                                goodsOrder.setSaveMoney(totalSaveMoney);
                            }
                            //购买增值服务 个数
                            int serviceNum=0;
                            //处理增值服务   //处理servicemoney
                            if (!StringUtils.isEmpty(addService)) {
                                //首先判断这些增值服务是否属于该商品
                                if (!goodsValueAddedServicesService.ifBelongGood(goodId, addService)) {//商品和增值服务的匹配关系出错
                                    map.put("code", "0");
                                    map.put("msg", "商品Id：" + goodId + "增值服务对象" + addService + "存在不匹配异常");
                                    return new JSONObject(map).toString();

                                }
                                //有增值服务则商品数量不能大于1
                                if (goodNum != 1) {
                                    map.put("code", 0);
                                    map.put("msg", "有增值服务则商品数量不能大于1");
                                    return new JSONObject(map).toString();
                                }
                                goodsOrder.setAddServiceId(addService);
                                //处理增值服务的价格总和
                                Map<String, Object> serviceMap = new HashMap<String, Object>();
                                serviceMap = valueAddedServicesService.serviceMoney(addService);
                                if (serviceMap.get("code").toString().equals("0")) {
                                    return CodeContent.getModelByCode(0, serviceMap.get("msg").toString(), null);
                                } else {//表示serviceMoney 正常计算
                                    totalServiceMoney = (BigDecimal) serviceMap.get("serviceMoney");
                                    goodsOrder.setServiceMoney(totalServiceMoney);
                                    purchaseOrderServiceNum+=(Integer)serviceMap.get("serviceNum");
                                    shopOrderServiceNum+=(Integer)serviceMap.get("serviceNum");
                                    serviceNum=(Integer)serviceMap.get("serviceNum");
                                }

                            }
                            goodsOrder.setCreateTime(new Date());
                            goodsOrder.setDdPrice(goodSpecification.getDdPrice());
                            goodsOrder.setNum(1);
                            goodsOrder.setGoodsId(goodId);
                            goodsOrder.setHasLmie(true);
                            goodsOrder.setGoodsName(good.getName());
                            goodsOrder.setGoodsSpecificationId(gooodSpecificationId);
                            goodsOrder.setSalePrice(goodSpecification.getMemberPrice());
                            goodsOrder.setMarketPrice(goodSpecification.getMarketPrice());
                            goodsOrder.setServiceNum(serviceNum);
                            //处理totalmoney
                            goodsOrder.setTotalMoney(goodSpecification.getMemberPrice().add(totalServiceMoney));
                            //处理实际支付的金钱
                            goodsOrder.setPayMoney(goodSpecification.getMemberPrice().add(totalServiceMoney).subtract(totalSaveMoney));
                            goodsOrder.setPurchaseOrderCode(purchaseOrder.getCode());
                            goodsOrderList.add(goodsOrder);
                        }

                    }
                } else {//无规格商品

                    //根据商品是否是需要mine号来进行不同的处理
                    if (!ifIslime) {//如果产品不需要    产品理应就没有增值服务
                        //预设3级订单
                        GoodsOrder goodsOrder = new GoodsOrder();
                        goodsOrder.setOrderState(OrderState.Create);
                        goodsOrder.setCreateTime(new Date());
                        goodsOrder.setGoodsName(good.getName());
                        goodsOrder.setUserId(loginUser.getId());
                        goodsOrder.setSeckill(false);
                        //判断是否有增服务
                        if (!StringUtils.isEmpty(addService)) {//表示需要增值服务
                            map.put("code", 0);
                            map.put("msg", "商品id为" + good.getId() + "的商品，基础属性不需要mine号，却有增值服务矛盾");
                            return new JSONObject(map).toString();
                        }

                        //无增值服务不需要进行相关增值服务的设置
                        goodsOrder.setNum(goodNum);
                          /*
                          * 处理用户的卡券
                          * */
                        UserCards userCards = userCardsService.goodCanUseUserCard(goodId, userCardsList);
                        BigDecimal totalMoney = good.getMemberPrice().multiply(new BigDecimal(goodNum));
                        BigDecimal payMoney = good.getMemberPrice().multiply(new BigDecimal(goodNum));
                        BigDecimal saveMoney = new BigDecimal(0);//初始话没有省钱
                        if (userCards != null) {//表示此商品有可用的券
                            goodsOrder.setCardId(userCards.getId());
                            payMoney = payMoney.subtract(userCards.getMoney());//减去卡券的钱
                            saveMoney = userCards.getMoney();
                            //去除掉这个卡券
                            userCardsService.removeUserCards(userCards, userCardsList);
                        } else {
                            goodsOrder.setSaveMoney(saveMoney);
                        }
                        goodsOrder.setDdPrice(good.getDdPrice());
                        goodsOrder.setHasLmie(false);
                        goodsOrder.setGoodsId(goodId);
                        goodsOrder.setSalePrice(good.getMemberPrice());
                        goodsOrder.setPurchaseOrderCode(purchaseOrder.getCode());
                        goodsOrder.setSaveMoney(saveMoney);
                        goodsOrder.setShopOrderCode(shopOrder.getCode());
                        goodsOrder.setPayMoney(payMoney);
                        goodsOrder.setTotalMoney(totalMoney);
                        String iCode = CodeContent.gCode("i", loginUser.getId());
                        goodsOrder.setCode(iCode);
                        goodsOrder.setServiceNum(0);
                        //将预设的三级订单加入到集合中，为之后做二级订单的构造做准备
                        goodsOrderList.add(goodsOrder);


                    } else {//此商品需要mine号

                        for (int k = 0; k < goodNum; k++) {//将每一个商品分拆为一个订单
                            GoodsOrder goodsOrder = new GoodsOrder();
                            BigDecimal totalMoney = new BigDecimal(0);
                            BigDecimal totalPayMoney = new BigDecimal(0);
                            BigDecimal totalSaveMoney = new BigDecimal(0);
                            BigDecimal totalServiceMoney = new BigDecimal(0);
                            String code = CodeContent.gCode("i", loginUser.getId() + k);
                            goodsOrder.setCode(code);
                            goodsOrder.setShopOrderCode(shopOrder.getCode());
                            goodsOrder.setOrderState(OrderState.Create);
                            goodsOrder.setUserId(loginUser.getId());
                            goodsOrder.setSeckill(false);
                            UserCards userCards = userCardsService.goodCanUseUserCard(goodId, userCardsList);
                            //去除掉这个卡券
                            if(userCards!=null){
                                goodsOrder.setCardId(userCards.getId());
                                totalSaveMoney = userCards.getMoney();
                                goodsOrder.setSaveMoney(totalSaveMoney);
                                userCardsService.removeUserCards(userCards, userCardsList);
                            } else {
                                goodsOrder.setSaveMoney(totalSaveMoney);
                            }
                            //购买增值服务 个数
                            int serviceNum=0;
                            //处理增值服务   //处理servicemoney
                            if (!StringUtils.isEmpty(addService)) {
                                //首先判断这些增值服务是否属于该商品
                                if (!goodsValueAddedServicesService.ifBelongGood(goodId, addService)) {//商品和增值服务的匹配关系出错
                                    map.put("code", "0");
                                    map.put("msg", "商品Id：" + goodId + "增值服务对象" + addService + "存在不匹配异常");
                                    return new JSONObject(map).toString();

                                }
                                if (goodNum != 1) {
                                    map.put("code", 0);
                                    map.put("msg", "含有增值服务的商品，数量只能为1");
                                    return new JSONObject(map).toString();
                                }
                                goodsOrder.setAddServiceId(addService);
                                //处理增值服务的价格总和
                                Map<String, Object> serviceMap = new HashMap<String, Object>();
                                serviceMap = valueAddedServicesService.serviceMoney(addService);
                                if (serviceMap.get("code").toString().equals("0")) {
                                    return CodeContent.getModelByCode(0, serviceMap.get("msg").toString(), null);
                                } else {//表示serviceMoney 正常计算
                                    purchaseOrderServiceNum+=(Integer)serviceMap.get("serviceNum");
                                    shopOrderServiceNum+=(Integer)serviceMap.get("serviceNum");
                                    totalServiceMoney = (BigDecimal) serviceMap.get("serviceMoney");
                                    goodsOrder.setServiceMoney(totalServiceMoney);
                                    serviceNum=(Integer)serviceMap.get("serviceNum");
                                }

                            }
                            goodsOrder.setServiceNum(serviceNum);
                            goodsOrder.setCreateTime(new Date());
                            goodsOrder.setDdPrice(good.getDdPrice());
                            goodsOrder.setNum(1);
                            goodsOrder.setGoodsId(goodId);
                            goodsOrder.setHasLmie(true);
                            goodsOrder.setGoodsName(good.getName());
                            goodsOrder.setSalePrice(good.getMemberPrice());

                            //处理totalmoney
                            goodsOrder.setTotalMoney(good.getMemberPrice().add(totalServiceMoney));
                            //处理实际支付的金钱
                            goodsOrder.setPayMoney(good.getMemberPrice().add(totalServiceMoney).subtract(totalSaveMoney));
                            goodsOrder.setPurchaseOrderCode(purchaseOrder.getCode());
                            goodsOrderList.add(goodsOrder);
                        }

                    }

                }
            }

        } else if (type == 2) {//购物车
                    /*
              * 从数据库中获取用户购物车中选中要用来结算的商品
              * */
            List<DdmShoppingCart> cartList = ddmShoppingCartDao.getByUserId(loginUser.getId());
            if (cartList == null || cartList.size() == 0) {//购物车中没有任何有效的商品数据
                map.put("code", 0);
                map.put("msg", "购物车为空");
                return new JSONObject(map).toString();
            }
           /*
           * 遍历购物车中的每一条记录
		   * */
            for (DdmShoppingCart ddmShoppingCart : cartList) {
                Map<String, Object> goodMap = new HashMap<String, Object>(); //good所用{} 05
                int goodId = ddmShoppingCart.getGoodsId();
                int goodNum = ddmShoppingCart.getCount();
                int gooodSpecificationId = 0;
                try {
                    gooodSpecificationId = ddmShoppingCart.getSpecificationId();
                } catch (Exception e) {
                    gooodSpecificationId = 0;
                }

                String addService = "";
                addService = ddmShoppingCart.getValueAddedServicesIds();


                //从数据库中获取商品对象
                DdmGoods good = ddmGoodsDao.getById(goodId);


                if (good == null) {
                    map.put("code", 0);
                    map.put("msg", "商品不存在");
                    return new JSONObject(map).toString();
                }

                if (good.getState() == false) {
                    map.put("code", 0);
                    map.put("msg", "商品状态无效");
                    map.put("msg", "无效商品");
                    return new JSONObject(map).toString();
                }

                boolean ifIslime = good.getIsImie();//商品是否必须一个唯一序列号


                  /*
                  * 处理规格
                  * */
                if (!StringUtils.isEmpty(gooodSpecificationId) && gooodSpecificationId != 0) {//带规格的商品
                    GoodSpecification goodSpecification = goodSpecificationDao.getById(gooodSpecificationId);

                    if (goodSpecification == null) {
                        map.put("code", 0);
                        map.put("msg", "商品规格不存在");
                        return new JSONObject(map).toString();
                    }
                    if (goodId != goodSpecification.getGoodId()) {
                        map.put("code", 0);
                        map.put("msg", "商品和商品规格对象不一致");
                        return new JSONObject(map).toString();

                    }
                    //根据商品是否是需要mine号来进行不同的处理
                    if (!ifIslime) {//如果产品不需要    产品理应就没有增值服务
                        //判断是否有增服务
                        if (!StringUtils.isEmpty(addService)) {//表示需要增值服务
                            map.put("code", 0);
                            map.put("msg", "商品id为" + good.getId() + "的商品，基础属性不需要mine号，却有增值服务矛盾");
                            return new JSONObject(map).toString();
                        }

                        //预设3级订单
                        GoodsOrder goodsOrder = new GoodsOrder();
                        goodsOrder.setOrderState(OrderState.Create);
                        goodsOrder.setCreateTime(new Date());
                        goodsOrder.setGoodsName(good.getName());
                        goodsOrder.setUserId(loginUser.getId());
                        goodsOrder.setSeckill(false);

                        goodsOrder.setGoodsSpecificationId(gooodSpecificationId);
                        //无增值服务不需要进行相关增值服务的设置
                        goodsOrder.setNum(goodNum);
                          /*
                          * 处理用户的卡券
                          * */
                        UserCards userCards = userCardsService.goodCanUseUserCard(goodId, userCardsList);
                        //由于商品没有mine号，所以商品的增值服务为0
                        BigDecimal totalMoney = goodSpecification.getMemberPrice().multiply(new BigDecimal(goodNum));
                        BigDecimal payMoney = goodSpecification.getMemberPrice().multiply(new BigDecimal(goodNum));
                        BigDecimal saveMoney = new BigDecimal(0);//初始话没有省钱
                        if (userCards != null) {//表示此商品有可用的券
                            goodsOrder.setCardId(userCards.getId());
                            payMoney = payMoney.subtract(userCards.getMoney());//减去卡券的钱
                            saveMoney = userCards.getMoney();
                            //去除掉这个卡券
                            userCardsService.removeUserCards(userCards, userCardsList);
                        } else {
                            goodsOrder.setSaveMoney(saveMoney);
                        }
                        goodsOrder.setDdPrice(goodSpecification.getDdPrice());
                        goodsOrder.setHasLmie(false);
                        goodsOrder.setMarketPrice(goodSpecification.getMarketPrice());
                        goodsOrder.setGoodsId(goodId);
                        goodsOrder.setSalePrice(goodSpecification.getMemberPrice());
                        goodsOrder.setPurchaseOrderCode(purchaseOrder.getCode());
                        goodsOrder.setSaveMoney(saveMoney);
                        goodsOrder.setShopOrderCode(shopOrder.getCode());
                        goodsOrder.setPayMoney(payMoney);
                        goodsOrder.setTotalMoney(totalMoney);
                        String iCode = CodeContent.gCode("i", loginUser.getId());
                        goodsOrder.setCode(iCode);
                        goodsOrder.setServiceNum(0);
                        //将预设的三级订单加入到集合中，为之后做二级订单的构造做准备
                        goodsOrderList.add(goodsOrder);


                    } else {//此商品需要mine号

                        for (int k = 0; k < goodNum; k++) {//将每一个商品分拆为一个订单
                            GoodsOrder goodsOrder = new GoodsOrder();
                            BigDecimal totalMoney = new BigDecimal(0);
                            BigDecimal totalPayMoney = new BigDecimal(0);
                            BigDecimal totalSaveMoney = new BigDecimal(0);
                            BigDecimal totalServiceMoney = new BigDecimal(0);
                            String code = CodeContent.gCode("i", loginUser.getId() + k);
                            goodsOrder.setCode(code);
                            goodsOrder.setShopOrderCode(shopOrder.getCode());
                            goodsOrder.setOrderState(OrderState.Create);
                            goodsOrder.setUserId(loginUser.getId());
                            goodsOrder.setSeckill(false);
                            UserCards userCards = userCardsService.goodCanUseUserCard(goodId, userCardsList);
                            //去除掉这个卡券
                            if (userCards != null) {
                                totalSaveMoney = userCards.getMoney();
                                goodsOrder.setSaveMoney(totalSaveMoney);
                                goodsOrder.setCardId(userCards.getId());
                                userCardsService.removeUserCards(userCards,userCardsList);
                            }else {
                                goodsOrder.setSaveMoney(totalSaveMoney);
                            }
                            //购买增值服务 个数
                            int serviceNum=0;
                            //处理增值服务   //处理servicemoney
                            if (!StringUtils.isEmpty(addService)) {
                                //首先判断这些增值服务是否属于该商品
                                if (!goodsValueAddedServicesService.ifBelongGood(goodId, addService)) {//商品和增值服务的匹配关系出错
                                    map.put("code", "0");
                                    map.put("msg", "商品Id：" + goodId + "增值服务对象" + addService + "存在不匹配异常");
                                    return new JSONObject(map).toString();

                                }
                                //有增值服务则商品数量不能大于1
                                if (goodNum != 1) {
                                    map.put("code", 0);
                                    map.put("msg", "有增值服务则商品数量不能大于1");
                                    return new JSONObject(map).toString();
                                }
                                goodsOrder.setAddServiceId(addService);
                                //处理增值服务的价格总和
                                Map<String, Object> serviceMap = new HashMap<String, Object>();
                                serviceMap = valueAddedServicesService.serviceMoney(addService);
                                if (serviceMap.get("code").toString().equals("0")) {
                                    return CodeContent.getModelByCode(0, serviceMap.get("msg").toString(), null);
                                } else {//表示serviceMoney 正常计算
                                    purchaseOrderServiceNum+=(Integer)serviceMap.get("serviceNum");
                                    shopOrderServiceNum+=(Integer)serviceMap.get("serviceNum");
                                    totalServiceMoney = (BigDecimal) serviceMap.get("serviceMoney");
                                    goodsOrder.setServiceMoney(totalServiceMoney);
                                    serviceNum=(Integer)serviceMap.get("serviceNum");
                                }

                            }
                            goodsOrder.setServiceNum(serviceNum);
                            goodsOrder.setCreateTime(new Date());
                            goodsOrder.setDdPrice(goodSpecification.getDdPrice());
                            goodsOrder.setNum(1);
                            goodsOrder.setGoodsId(goodId);
                            goodsOrder.setHasLmie(true);
                            goodsOrder.setGoodsName(good.getName());
                            goodsOrder.setGoodsSpecificationId(gooodSpecificationId);
                            goodsOrder.setSalePrice(goodSpecification.getMemberPrice());
                            goodsOrder.setMarketPrice(goodSpecification.getMarketPrice());
                            //处理totalmoney
                            goodsOrder.setTotalMoney(goodSpecification.getMemberPrice().add(totalServiceMoney));
                            //处理实际支付的金钱
                            goodsOrder.setPayMoney(goodSpecification.getMemberPrice().add(totalServiceMoney).subtract(totalSaveMoney));
                            goodsOrder.setPurchaseOrderCode(purchaseOrder.getCode());
                            goodsOrderList.add(goodsOrder);
                        }

                    }
                } else {//无规格商品

                    //根据商品是否是需要mine号来进行不同的处理
                    if (!ifIslime) {//如果产品不需要    产品理应就没有增值服务
                        //预设3级订单
                        GoodsOrder goodsOrder = new GoodsOrder();
                        goodsOrder.setOrderState(OrderState.Create);
                        goodsOrder.setCreateTime(new Date());
                        goodsOrder.setGoodsName(good.getName());
                        goodsOrder.setUserId(loginUser.getId());
                        goodsOrder.setSeckill(false);
                        //判断是否有增服务
                        if (!StringUtils.isEmpty(addService)) {//表示需要增值服务
                            map.put("code", 0);
                            map.put("msg", "商品id为" + good.getId() + "的商品，基础属性不需要mine号，却有增值服务矛盾");
                            return new JSONObject(map).toString();
                        }

                        //无增值服务不需要进行相关增值服务的设置
                        goodsOrder.setNum(goodNum);
                          /*
                          * 处理用户的卡券
                          * */
                        UserCards userCards = userCardsService.goodCanUseUserCard(goodId, userCardsList);
                        BigDecimal totalMoney = good.getMemberPrice().multiply(new BigDecimal(goodNum));
                        BigDecimal payMoney = good.getMemberPrice().multiply(new BigDecimal(goodNum));
                        BigDecimal saveMoney = new BigDecimal(0);//初始话没有省钱
                        if (userCards != null) {//表示此商品有可用的券
                            goodsOrder.setCardId(userCards.getId());
                            payMoney = payMoney.subtract(userCards.getMoney());//减去卡券的钱
                            saveMoney = userCards.getMoney();
                            //去除掉这个卡券
                            userCardsService.removeUserCards(userCards, userCardsList);
                        } else {
                            goodsOrder.setSaveMoney(saveMoney);

                        }
                        goodsOrder.setDdPrice(good.getDdPrice());
                        goodsOrder.setHasLmie(false);

                        goodsOrder.setGoodsId(goodId);
                        goodsOrder.setSalePrice(good.getMemberPrice());
                        goodsOrder.setPurchaseOrderCode(purchaseOrder.getCode());
                        goodsOrder.setSaveMoney(saveMoney);
                        goodsOrder.setShopOrderCode(shopOrder.getCode());
                        goodsOrder.setPayMoney(payMoney);
                        goodsOrder.setTotalMoney(totalMoney);
                        String iCode = CodeContent.gCode("i", loginUser.getId());
                        goodsOrder.setServiceNum(0);
                        goodsOrder.setCode(iCode);
                        //将预设的三级订单加入到集合中，为之后做二级订单的构造做准备
                        goodsOrderList.add(goodsOrder);


                    } else {//此商品需要mine号

                        for (int k = 0; k < goodNum; k++) {//将每一个商品分拆为一个订单
                            GoodsOrder goodsOrder = new GoodsOrder();
                            BigDecimal totalMoney = new BigDecimal(0);
                            BigDecimal totalPayMoney = new BigDecimal(0);
                            BigDecimal totalSaveMoney = new BigDecimal(0);
                            BigDecimal totalServiceMoney = new BigDecimal(0);
                            String code = CodeContent.gCode("i", loginUser.getId() + k);
                            goodsOrder.setCode(code);
                            goodsOrder.setShopOrderCode(shopOrder.getCode());
                            goodsOrder.setOrderState(OrderState.Create);
                            goodsOrder.setUserId(loginUser.getId());
                            goodsOrder.setSeckill(false);
                            UserCards userCards = userCardsService.goodCanUseUserCard(goodId, userCardsList);
                            //去除掉这个卡券
                            if(userCards!=null){
                                goodsOrder.setCardId(userCards.getId());
                                totalSaveMoney = userCards.getMoney();
                                goodsOrder.setSaveMoney(totalSaveMoney);
                                userCardsService.removeUserCards(userCards, userCardsList);
                            } else {
                                goodsOrder.setSaveMoney(totalSaveMoney);
                            }
                            //购买增值服务 个数
                            int serviceNum=0;
                            //处理增值服务   //处理servicemoney
                            if (!StringUtils.isEmpty(addService)) {
                                //首先判断这些增值服务是否属于该商品
                                if (!goodsValueAddedServicesService.ifBelongGood(goodId, addService)) {//商品和增值服务的匹配关系出错
                                    map.put("code", "0");
                                    map.put("msg", "商品Id：" + goodId + "增值服务对象" + addService + "存在不匹配异常");
                                    return new JSONObject(map).toString();

                                }
                                if (goodNum != 1) {
                                    map.put("code", 0);
                                    map.put("msg", "含有增值服务的商品，数量只能为1");
                                    return new JSONObject(map).toString();
                                }
                                goodsOrder.setAddServiceId(addService);
                                //处理增值服务的价格总和
                                Map<String, Object> serviceMap = new HashMap<String, Object>();
                                serviceMap = valueAddedServicesService.serviceMoney(addService);
                                if (serviceMap.get("code").toString().equals("0")) {
                                    return CodeContent.getModelByCode(0, serviceMap.get("msg").toString(), null);
                                } else {//表示serviceMoney 正常计算
                                    totalServiceMoney = (BigDecimal) serviceMap.get("serviceMoney");
                                    goodsOrder.setServiceMoney(totalServiceMoney);
                                    purchaseOrderServiceNum+=(Integer)serviceMap.get("serviceNum");
                                    shopOrderServiceNum+=(Integer)serviceMap.get("serviceNum");
                                    serviceNum=(Integer)serviceMap.get("serviceNum");
                                }

                            }
                            goodsOrder.setCreateTime(new Date());
                            goodsOrder.setDdPrice(good.getDdPrice());
                            goodsOrder.setNum(1);
                            goodsOrder.setGoodsId(goodId);
                            goodsOrder.setHasLmie(true);
                           // goodsOrder.setHasComment(false);
                            goodsOrder.setGoodsName(good.getName());
                            goodsOrder.setSalePrice(good.getMemberPrice());

                            goodsOrder.setServiceNum(serviceNum);
                            //处理totalmoney
                            goodsOrder.setTotalMoney(good.getMemberPrice().add(totalServiceMoney));
                            //处理实际支付的金钱
                            goodsOrder.setPayMoney(good.getMemberPrice().add(totalServiceMoney).subtract(totalSaveMoney));
                            goodsOrder.setPurchaseOrderCode(purchaseOrder.getCode());
                            goodsOrderList.add(goodsOrder);
                        }

                    }

                }



            }

        }





         /*
               * 开始正常生成各级订单
               * */
        BigDecimal totalMoney = new BigDecimal(0);//没有任何优惠需要支付的金钱
        BigDecimal totalPayMoney = new BigDecimal(0);   //实际需要支付的金钱
        BigDecimal totalSaveMoney = new BigDecimal(0);//一共节省下来的金钱
        StringBuffer userCardsIdString = new StringBuffer();  //用户优惠券id拼接（userCards）

        for (GoodsOrder goodsOrder : goodsOrderList) {
            //计算一共要支付的金钱
            totalMoney = goodsOrder.getTotalMoney().add(totalMoney);
            //计算实际需要支付的金钱
            totalPayMoney = goodsOrder.getPayMoney().add(totalPayMoney);
            //计算实际节省下来的钱
            totalSaveMoney = goodsOrder.getSaveMoney().add(totalSaveMoney);
            //构造usercardsid字符串
            if (goodsOrder.getCardId() != null) {
                userCardsIdString.append(goodsOrder.getCardId() + ",");

            }

        }

        /*
        * 补充二级订单
        * */
        shopOrder.setTotalMoney(totalMoney);
        shopOrder.setSaveMoney(totalSaveMoney);
        shopOrder.setPayMoney(totalPayMoney);
        //shopOrder.setHasComment(false);
        shopOrder.setServiceNum(shopOrderServiceNum);
       /*
       * 补充一级订单
       * */
        purchaseOrder.setTotalMoney(totalMoney);
        purchaseOrder.setSaveMoney(totalSaveMoney);
        purchaseOrder.setPayMoney(totalPayMoney);
        purchaseOrder.setServiceNum(purchaseOrderServiceNum);
        /*
        *  添加三级订单，二级订单，三级订单  将卡券也使用掉
        * */
        List<DdmShoppingCart> cartList = ddmShoppingCartDao.getByUserId(loginUser.getId());
        purchaseOrderService.submitOrder(purchaseOrder, shopOrder, goodsOrderList, type, cartList);

        //改购物车 选中的商品 状态 为 提交订单
        if (type == 2) {
            List<DdmShoppingCart> cartList2 = ddmShoppingCartDao.getByUserId(loginUser.getId());
            if (cartList2 != null && cartList2.size() >0) {//购物车中没有任何有效的商品数据
               /*
           * 遍历购物车中的每一条记录
		   * */
                for (DdmShoppingCart ddmShoppingCart : cartList2) {
                    //ffl 更改购物车 选中的商品 状态 为 提交订单
                    ddmShoppingCart.setShoppingCartState(ShoppingCartState.HaveAlreadyBought);
                    ddmShoppingCartDao.modify(ddmShoppingCart);
                }
            }

        }
        Map<String, Object> storeMap = new HashMap<String, Object>();
        map.put("payMoney", purchaseOrder.getPayMoney());
        map.put("pCode", purchaseOrder.getCode());
        map.put("code", 1);
        map.put("msg", "success");
        map.put("payType", payType);
        map.put("deliveryType", deliveryType);
        if (ddmShop != null) {
            map.put("point", ddmShop.getLongitudeAndLatitude());
            map.put("shopAddress", ddmShop.getLocation());
            map.put("shopName", ddmShop.getName());
        }
        return new JSONObject(map).toString();
    }


    /**
     * 支付之前用户取消订单
     * ffl
     * 2017-11-06 15:35:22
     *
     * @param token
     * @param cancelReasonValue 取消原因
     * @param purchaseOrderId   一级订单id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/cancelOrder.json", produces = "application/json;charset=UTF-8")
    public String cancelOrder(String companyCode,String token, String cancelReasonValue, String purchaseOrderId) {
        return shopOrderService.cancelOrder(token, cancelReasonValue, purchaseOrderId);
    }

    /**
     * 支付之后停止购买
     * ffl
     * 2017-11-06 15:38:13
     *
     * @param token
     * @param shopOrderId       二级订单id
     * @param cancelReasonValue 取消原因
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/stopOrder.json", produces = "application/json;charset=UTF-8")
    public String stopOrder(String companyCode,String token, String shopOrderId, String cancelReasonValue) {
        return shopOrderService.stopOrder(token, shopOrderId, cancelReasonValue);
    }

    /**
     * 我的订单
     * ffl
     * 2017-11-07 17:23:39
     *
     * @param token
     * @param orderStateValue 订单状态值
     * @param hasComment      是否可以评论
     * @param pageNum         第几页
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/orderList.json", produces = "application/json;charset=UTF-8")
    public String orderList(String companyCode,String token, String orderStateValue, String hasComment, String pageNum) {
        return shopOrderService.orderList(token, orderStateValue, hasComment, pageNum);
    }

    /**
     * 订单详情
     * ffl
     * 2017-11-07 17:29:41
     *
     * @param token
     * @param shopOrderId 仓库或店铺 订单（二级订单） ID
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/orderDetail.json", produces = "application/json;charset=UTF-8")
    public String orderDetail(String companyCode,String token, String shopOrderId) {
        return shopOrderService.orderDetail(token, shopOrderId);
    }



    protected User getCurrentUser(){
        if(SecurityUtils.getSubject().getPrincipal() instanceof String) {
            SecurityUtils.getSubject().logout();
            return null;
        }

        Login login = (Login) SecurityUtils.getSubject().getPrincipal();
        if(login != null){
            User user = userService.getByName(login.getName());
            if(user == null || UserState.Invalid.equals(user.getUserState()) || !user.getPassword().equals(login.getPwd())){
                SecurityUtils.getSubject().logout();
                return null;
            }else
                return user;
        }

        return null;
    }


    /**
     * 确认收货  接口
     * ffl
     * 2017-11-08 20:22:48
     * @param token
     * @param shopOrderId
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/confirmReceiptOrder.json",produces="application/json;charset=UTF-8")
    public String confirmReceiptOrder(String companyCode,String token,String shopOrderId){
        return shopOrderService.confirmReceiptOrder(token, shopOrderId);
    }


    /**
     * 订单状态下的 订单数量
     * ffl
     * 2017-11-09 11:04:24
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/purchaseOrderNum.json",produces="application/json;charset=UTF-8")
    public String  purchaseOrderNum(String companyCode,String token){
        return shopOrderService.purchaseOrderNum(token);
    }


    /**
     * 去评价 展示商品
     * ffl
     * 2017-11-09 17:30:57
     * @param token
     * @param shopOrderId
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/orderComment.json",produces="application/json;charset=UTF-8")
    public String orderComment(String companyCode,String token,String shopOrderId){
        return shopOrderService.orderComment(token, shopOrderId);
    }


    /**
     * 退款/售后申请
     * ffl
     * 2017-11-09 20:32:27
     * @param token
     * @param goodsOrderId
     * @param refundTypeValue
     * @param refundReasonValue
     * @param refundMoney
     * @param refundExplain
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/refundApply.json",produces="application/json;charset=UTF-8")
    public String refundApply(String companyCode,String token ,String goodsOrderId,String refundTypeValue,String refundReasonValue,String refundMoney,String refundExplain,String picture ) {
        return  refundLogService.refundApply(token, goodsOrderId, refundTypeValue, refundReasonValue, refundMoney, refundExplain,picture);
    }


    /**
     * 退款详情
     * ffl
     * 2017-11-10 14:18:57
     * @param token
     * @param goodsOrderId
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/refundDetail.json",produces="application/json;charset=UTF-8")
    public String refundDetail(String companyCode,String token,String goodsOrderId){
        return shopOrderService.refundDetail(token, goodsOrderId);
    }

    /**
     * 用户放弃售后/退款  申 请
     * ffl
     * 2017-11-11 11:33:30
     * @param token
     * @param refundId
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/cancelRefund.json",produces="application/json;charset=UTF-8")
    public String refundShutDown(String companyCode,String token,String refundId){
        return refundLogService.refundShutDown(token, refundId);
    }

    /**
     * 退款/售后 列表
     * ffl
     * 2017-11-11 19:35:45
     * @param token
     * @param pageNum
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/refundList.json",produces="application/json;charset=UTF-8")
    public String refundList(String companyCode,String token,String pageNum){
        return shopOrderService.refundList(token, pageNum);
    }
}
